package com.medicine.product.utils;

/**
 * @author lynn
 * @version 1.0
 * @date 2021/11/1
 */

import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import cz.vutbr.web.css.MediaSpec;
import org.fit.cssbox.css.CSSNorm;
import org.fit.cssbox.css.DOMAnalyzer;
import org.fit.cssbox.layout.BrowserCanvas;

import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.jsoup.nodes.Document;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.UUID;
import org.jsoup.nodes.Element;


public class FreemarkerUtils {

    private static String mediaType = "screen";
    private static Dimension windowSize = new Dimension(1200, 600);
    private static boolean cropWindow = false;
    private static boolean loadImages = true;
    private static boolean loadBackgroundImages = true;


    public static void turnImage(Document doc) throws IOException {
        /*java.util.ArrayList<Element> elementList = doc.getElementById("feedCardContent").getElementsByAttributeValue("class", "feed-card-item");//获取元素节点等

        for(Element e : elementList){
            System.out.println(e.getElementsByTag("h2").first().getElementsByTag("a").text());
            System.out.println(e.getElementsByTag("h2").first().getElementsByTag("a").attr("href"));
        }*/
        /*infoListEle.forEach(element -> {
            System.out.println(element.getElementsByTag("h2").first().getElementsByTag("a").text());
            System.out.println(element.getElementsByTag("h2").first().getElementsByTag("a").attr("href"));
        });*/

        W3CDom w3CDom = new W3CDom();
        org.w3c.dom.Document w3cDoc = w3CDom.fromJsoup(doc);
        DOMAnalyzer da = new DOMAnalyzer(w3cDoc, null);
        MediaSpec media = new MediaSpec(mediaType);
        media.setDimensions((float) windowSize.width, (float) windowSize.height);
        media.setDeviceDimensions((float) windowSize.width, (float) windowSize.height);
        da.setMediaSpec(media);
        da.attributesToStyles();
        da.addStyleSheet((URL) null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT);
        da.addStyleSheet((URL) null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT);
        da.addStyleSheet((URL) null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT);
        da.getStyleSheets();
        BrowserCanvas contentCanvas = new BrowserCanvas(w3cDoc.getDocumentElement(), da, null);
        contentCanvas.setAutoMediaUpdate(false);
        contentCanvas.getConfig().setClipViewport(cropWindow);
        contentCanvas.getConfig().setLoadImages(loadImages);
        contentCanvas.getConfig().setLoadBackgroundImages(loadBackgroundImages);
        contentCanvas.createLayout(new Dimension(297,210));
        OutputStream out = new FileOutputStream("d:\\"+ UUID.randomUUID().toString() +".png");
        ImageIO.write(contentCanvas.getImage(), "png", out);
    }

    public static void main(String[] args) throws Exception {
        //Htmlunit模拟的浏览器，设置css,js等支持及其它的一些简单设置
        WebClient browser = new WebClient();
        browser.getOptions().setCssEnabled(false);
        browser.getOptions().setJavaScriptEnabled(true);
        browser.getOptions().setThrowExceptionOnScriptError(false);
        browser.setAjaxController(new NicelyResynchronizingAjaxController());
        browser.getOptions().setThrowExceptionOnScriptError(false);//当JS执行出错的时候是否抛出异常, 这里选择不需要
        browser.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常, 这里选择不需要
        browser.getOptions().setActiveXNative(false);
        //获取页面
        HtmlPage htmlPage = browser.getPage("http://scrm.uatw.vdaifu.com/soa/test.htm");
        //设置等待js的加载时间
        browser.waitForBackgroundJavaScript(3000);
        //使用xml的方式解析获取到jsoup的document对象
        Document doc = Jsoup.parse(htmlPage.asXml());
        FreemarkerUtils.turnImage(doc);
    }


}


